package cn.edu.zzuli;


import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

class MyThread implements  Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        TimeUnit.SECONDS.sleep(1);
        System.out.println("****线程others开始计算*****");
        TimeUnit.SECONDS.sleep(4);

        return 1024;
    }
}
/**
 * @Date: 2021/3/7 20:30
 * @Author: 绯色下午茶
 * @Description:   java多线程中第3种获得多线程的方式
 */
public class CallableDemo {
    public static void main(String []args) throws Exception {
        System.out.println("*******我想计算 ");
//        new MyThread().call();//这种无法获得返回值
        FutureTask<Integer> task = new FutureTask<>(new MyThread());
        new Thread(task,"线程A").start();
        new Thread(task,"线程B").start();
        /**
         * 这种可以获得返回值
         */
        System.out.println(task.get());

        System.out.println(Thread.currentThread().getName()+"*****计算完成，结果是： 20 + "+task.get()+" = "+20+task.get());
    }
}
